package org.mch.swingm.event;

import java.awt.event.ActionEvent;

import javax.swing.AbstractButton;

import org.mch.comm.exec.MsgException;
import org.mch.comm.itf.IMLogger;
import org.mch.comm.loging.MComLogger;
import org.mch.swingm.itf.IMEvent;

/**
 * 描述：基础事件类；<BR/>
 * 
 * @author ZhangLi；mchweb.net；<BR/>
 *
 */
public abstract class MBaseEvent implements IMEvent {
	private static final long serialVersionUID = -7947168761157774420L;

	protected IMLogger log = new MComLogger(getClass());

	/**
	 * 按钮点击事件执行的方法，由于Swing中的按钮Click事件太敏感，所以这里将Click改为Released；
	 * 
	 * @param e
	 */
	protected abstract void onButtonClick(ActionEvent e) throws Exception;

	@Override
	public final void actionPerformed(ActionEvent e) {
		try {
			if (e.getSource() instanceof AbstractButton && ((AbstractButton) e.getSource()).isEnabled()) {
				onButtonClick(e);
			}
		} catch (Exception ex) {
			handleException(ex);
		}
	}

	protected void handleException(Throwable e) {
		if (e instanceof MsgException) {
			log.info(e.getMessage());
			log.showMsg(e.getMessage());
		} else {
			log.error(e);
		}
	}
}
